package com.itic.hpp.self;

import com.itic.appbase.framework.Base.BaseCode;
import com.itic.appbase.framework.common.constants.ResponseCode;
import com.itic.appbase.framework.utils.StringHelper;
import com.itic.appbase.framework.utils.ValueHelper;
import com.itic.mobile.base.BaseMobile;
import com.itic.system.self.selfLifting.persistence.model.SelfLifting;
import com.itic.system.self.selfLifting.persistence.model.SelfLiftingExample;
import com.itic.system.self.selfLifting.service.SelfLiftingService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.itic.system.user.user.persistence.model.HppUser;
import com.itic.system.user.user.service.HppUserService;
import com.itic.system.vo.SelfliftDelCon;

@Controller
@RequestMapping(value = "/hpp/self/lifting")
public class SelfLiftingInterface extends BaseMobile {

	@Autowired
	private SelfLiftingService selfLiftingService;
	@Autowired
	private HppUserService hppUserService;

	private static String[] ignoreProperties = new String[] { "createUser", "isEffective", "updateTime", "orgCode",
			"reorder", "note", "createTime" };

	/**
	 * 添加自提点
	 * 
	 * @param request
	 * @param response
	 * @param selfName
	 * @param provinceId
	 * @param proviceName
	 * @param cityId
	 * @param cityName
	 * @param regionId
	 * @param regionName
	 * @param address
	 * @param phone
	 * @param seftCode
	 * @param userId
	 * @return
	 */
	@RequestMapping(value = "/add")
	@ResponseBody
	public Map<String, Object> add(HttpServletRequest request, HttpServletResponse response, String selfName,
			String provinceId, String proviceName, String cityId, String cityName, String regionId, String regionName,
			String address, String phone, String seftCode, String userId) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		String errorCode = PARAM_ERROR_CODE;
		String errorMsg = PARAM_ERROR_MSG;
		try {
			if (StringUtils.isNoneBlank(selfName, address, phone, userId)) {
				String result = this.selfLiftingService.addSelfLifting(selfName, provinceId, proviceName, cityId,
						cityName, regionId, regionName, address, phone, seftCode, userId);
				if (result.equals(ResponseCode.SUCCESS.getCode())) {
					errorCode = SUCCESS_ERROR_CODE;
					errorMsg = SUCCESS_ERROR_MSG;
				}
			}
		} catch (Exception e) {
			errorCode = SERVER_ERROR_CODE;
			errorMsg = SERVER_ERROR_MSG;
			logException("SelfLiftingInterface", "add", e.toString(), "添加自提点", e);
		}
		resultMap.put(CODE, errorCode);
		resultMap.put(MSG, errorMsg);
		isCrrossDomain(response);
		return resultMap;
	}

	/**
	 * 修改自提点
	 * 
	 * @param request
	 * @param response
	 * @param selfName
	 * @param provinceId
	 * @param proviceName
	 * @param cityId
	 * @param cityName
	 * @param regionId
	 * @param regionName
	 * @param address
	 * @param phone
	 * @param seftCode
	 * @param userId
	 * @param selfId
	 * @return
	 */
	@RequestMapping(value = "/edit")
	@ResponseBody
	public Map<String, Object> edit(HttpServletRequest request, HttpServletResponse response, String selfName,
			String provinceId, String proviceName, String cityId, String cityName, String regionId, String regionName,
			String address, String phone, String seftCode, String userId, String selfId) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			if (StringUtils.isNoneBlank(selfId, selfName, address, phone, userId)) {
				// 处理修改自提点业务逻辑
				resultMap = this.selfLiftingService.editSelf(selfId, selfName, provinceId, proviceName, cityId,
						cityName, regionId, regionName, address, phone, seftCode, userId);
			} else {
				resultMap.put(BaseCode.CODE, BaseCode.PARAM_ERROR_CODE);
				resultMap.put(BaseCode.MSG, BaseCode.PARAM_ERROR_MSG);
			}
		} catch (Exception e) {
			resultMap.put(BaseCode.CODE, BaseCode.SERVER_ERROR_CODE);
			resultMap.put(BaseCode.MSG, BaseCode.SERVER_ERROR_MSG);
			logException("SelfLiftingInterface", "edit", e.toString(), "编辑自提点", e);
		}

		isCrrossDomain(response);
		return resultMap;
	}

	/**
	 * 自提点列表获取
	 * 
	 * @param userId
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/list")
	@ResponseBody
	public Map<String, Object> list(String userId, HttpServletRequest request, HttpServletResponse response) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		String errorCode = PARAM_ERROR_CODE;
		String errorMsg = PARAM_ERROR_MSG;
		try {
			if (StringHelper.isNotBlank(userId)) {
				HppUser hppUser = hppUserService.get(userId);
				if (hppUser != null) {
		    		SelfLiftingExample example = new SelfLiftingExample();
					this.handlePageQueryDefault(request,example);
					List<SelfLifting> list = selfLiftingService.getListByUserId(example, userId);
					List<Map<String, Object>> listMap= ValueHelper.tranList2MapList(list, ignoreProperties);
					for(Map<String, Object> map :listMap){
						String provinceName =(String) map.get("provinceName");
						String cityName =(String) map.get("cityName");
						String regionName =(String) map.get("regionName");
						map.put("region",provinceName+cityName+regionName);
					}
					resultMap.put(DATA, listMap);
					errorCode = SUCCESS_ERROR_CODE;
					errorMsg = SUCCESS_ERROR_MSG;
				} else {
					errorCode = USER_ERROR_CODE;
					errorMsg = USER_ERROR_MSG;
				}
			}
		} catch (Exception e) {
			errorCode = SERVER_ERROR_CODE;
			errorMsg = SERVER_ERROR_MSG;
			logException("SelfLiftingInterface", "list", e.toString(), "自提点列表", e);
		}
		resultMap.put(CODE, errorCode);
		resultMap.put(MSG, errorMsg);
		isCrrossDomain(response);
		return resultMap;
	}

	/**
	 * 删除
	 * 
	 * @param userId
	 * @param id
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/del")
	@ResponseBody
	public Map<String, Object> del(String userId, String id, HttpServletRequest request, HttpServletResponse response) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		String errorCode = PARAM_ERROR_CODE;
		String errorMsg = PARAM_ERROR_MSG;
		try {
			if (StringHelper.isNotBlank(userId) && StringHelper.isNotBlank(id)) {
				HppUser hppUser = hppUserService.get(userId);
				if (hppUser != null) {
					Boolean isOwn = selfLiftingService.isOwn(userId, id);
					if (isOwn) {
						SelfliftDelCon con = new SelfliftDelCon();
						con.setId(id);
						con.setUserId(userId);
						selfLiftingService.delByuserIdandId(con);
						errorCode = SUCCESS_ERROR_CODE;
						errorMsg = SUCCESS_ERROR_MSG;
					} else {
						errorCode = USER_AUTH_ERROR_CODE;
						errorMsg = USER_AUTH_ERROR_MSG;
					}
				} else {
					errorCode = USER_ERROR_CODE;
					errorMsg = USER_ERROR_MSG;
				}
			}
		} catch (Exception e) {
			errorCode = SERVER_ERROR_CODE;
			errorMsg = SERVER_ERROR_MSG;
			logException("SelfLiftingInterface", "del", e.toString(), "自提点删除", e);
		}
		resultMap.put(CODE, errorCode);
		resultMap.put(MSG, errorMsg);
		return resultMap;
	}

	/**
	 * 名称查询
	 * @param userId
	 * @param name
	 * @param type
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/query")
	@ResponseBody
	public Map<String, Object> query(String userId, String name, String type, HttpServletRequest request,
			HttpServletResponse response) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		String errorCode = PARAM_ERROR_CODE;
		String errorMsg = PARAM_ERROR_MSG;
		try {
			if (StringUtils.isNotBlank(userId)) {
				HppUser hppUser = hppUserService.get(userId);
				if (hppUser != null) {
					List<SelfLifting> list = selfLiftingService.quryBycondition(userId, name, type);
					resultMap.put(DATA, ValueHelper.tranList2MapList(list, ignoreProperties));
					errorCode = SUCCESS_ERROR_CODE;
					errorMsg = SUCCESS_ERROR_MSG;
				} else {
					errorCode = USER_ERROR_CODE;
					errorMsg = USER_ERROR_MSG;
				}
			}
		} catch (Exception e) {
			errorCode = SERVER_ERROR_CODE;
			errorMsg = SERVER_ERROR_MSG;
			logException("SelfLiftingInterface", "query", e.toString(), "自提点查询", e);
		}
		resultMap.put(CODE, errorCode);
		resultMap.put(MSG, errorMsg);
		return resultMap;
	}


	/**
	 *  自提点编辑详情页数据
	 * @param selfId
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/editDetail")
	@ResponseBody
	public Map<String, Object> editDetail(String selfId,  HttpServletRequest request,
									 HttpServletResponse response) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			if (StringUtils.isNotBlank(selfId)) {
				SelfLifting selfLifting = this.selfLiftingService.get(selfId);
				if(selfLifting!=null){
					Map<String,Object> data = new HashMap<>();
					data.put("name",selfLifting.getName());
					data.put("regionId",selfLifting.getRegionId());
					data.put("regionName",selfLifting.getRegionName());
					data.put("cityId",selfLifting.getCityId());
					data.put("cityName",selfLifting.getCityName());
					data.put("provinceId",selfLifting.getProvinceId());
					data.put("provinceName",selfLifting.getProvinceName());
					data.put("phone",selfLifting.getPhone());
					data.put("address",selfLifting.getAddress());
					resultMap.put(BaseCode.DATA,data);
					resultMap.put(BaseCode.CODE, BaseCode.SUCCESS_ERROR_CODE);
					resultMap.put(BaseCode.MSG, BaseCode.SUCCESS_ERROR_MSG);
				}else{
					//提示自提点不存在
					resultMap.put(BaseCode.CODE, BaseCode.HPP_SEFT_NOT_EXIST_CODE);
					resultMap.put(BaseCode.MSG, BaseCode.HPP_SEFT_NOT_EXIST_MSG);
				}
			}else{
				resultMap.put(BaseCode.CODE, BaseCode.PARAM_ERROR_CODE);
				resultMap.put(BaseCode.MSG, BaseCode.PARAM_ERROR_MSG);
			}
		} catch (Exception e) {
			resultMap.put(BaseCode.CODE, BaseCode.SERVER_ERROR_CODE);
			resultMap.put(BaseCode.MSG, BaseCode.SERVER_ERROR_MSG);
			logException("SelfLiftingInterface", "editDetail", e.toString(), "自提点编辑详情页", e);
		}
		return resultMap;
	}

}
